package com.gsi.webstore.platform.storefront.pipelet.orderprocess;

import com.gsi.webstore.platform.adapter.storelocator.capi.StoreLocatorManager;
import com.gsi.webstore.platform.adapter.storelocator.internal.client.data.Storefront;
import com.intershop.beehive.core.capi.naming.NamingMgr;
import com.intershop.beehive.core.capi.pipeline.Pipelet;
import com.intershop.beehive.core.capi.pipeline.PipelineDictionary;
import com.intershop.beehive.core.capi.pipeline.PipeletExecutionException;
import com.intershop.beehive.core.capi.pipeline.PipelineInitializationException;
import com.intershop.beehive.bts.capi.orderprocess.LineItem;
import com.intershop.beehive.bts.capi.orderprocess.basket.Basket;
import com.intershop.beehive.core.capi.request.Request;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 
 */
public class MapStorefrontWithCartLineItems extends Pipelet
{
    /**
     * Constant used to access the pipeline dictionary with key 'Cart'
     */
    public static final String DN_CART = "Cart";
    
    /**
     * Constant used to access the pipeline dictionary with key 'CurrentRequest'
     */
    public static final String DN_CURRENT_REQUEST = "CurrentRequest";
    
    /**
     * Constant used to access the pipeline dictionary with key
     * 'StorefrontCartLineItemsMap'
     */
    public static final String DN_STOREFRONT_CART_LINE_ITEMS_MAP = "StorefrontCartLineItemsMap";
    
    private StoreLocatorManager storeLocatorMgr = null;
    private String storeFrontIdAttribute = "STORE_ID";
    
    public void init() throws PipelineInitializationException
    {
        storeLocatorMgr = (StoreLocatorManager)NamingMgr.getManager(StoreLocatorManager.class);
    }

    /**
     * The pipelet's execute method is called whenever the pipelets gets
     * executed in the context of a pipeline and a request. The pipeline
     * dictionary valid for the currently executing thread is provided as a
     * parameter.
     * 
     * @param dict
     *            The pipeline dictionary to be used.
     * @throws PipeletExecutionException
     *             Thrown in case of severe errors that make the pipelet execute
     *             impossible (e.g. missing required input data).
     */
    public int execute(PipelineDictionary dict) throws PipeletExecutionException
    {
        // lookup 'CurrentRequest' in pipeline dictionary
        Request currentRequest = (Request)dict.get(DN_CURRENT_REQUEST);
        if (null == currentRequest)
        {
            throw new PipeletExecutionException(
                            "Mandatory input parameter 'CurrentRequest' not available in pipeline dictionary.");
        }

        // lookup 'Cart' in pipeline dictionary
        Basket cart = (Basket)dict.get(DN_CART);
        if (null == cart)
        {
            throw new PipeletExecutionException(
                            "Mandatory input parameter 'Cart' not available in pipeline dictionary.");
        }
        
        String localeId = null;
        Long storeId = null;
        Storefront storeFront;
        Map<Long, Storefront> storeFrontMap = new HashMap<Long, Storefront>();
        Map<String, Storefront> storefrontCartLineItemsMap = new HashMap<String, Storefront>();

        localeId = currentRequest.getLocale().getLocaleID();
        Iterator<LineItem> lineItemsIterator = cart.createProductLineItemsOrderedByPositionIterator();
        
        while(lineItemsIterator != null && lineItemsIterator.hasNext())
        {
            LineItem currentLineItem = lineItemsIterator.next();
            storeId = (Long)currentLineItem.getAttribute(storeFrontIdAttribute);
            // if(storeId != null && )
            if (storeFrontMap.get(storeId) == null)
            {
                if (storeId != null)
                {
                    storeFront = storeLocatorMgr.getStorefront(storeId, localeId);
                    storeFrontMap.put(storeId, storeFront);
                    storefrontCartLineItemsMap.put(currentLineItem.getUUID(), storeFront);
                }
            }
            else
            {
                if (storeId != null)
                {
                    storeFront = storeFrontMap.get(storeId);
                    storefrontCartLineItemsMap.put(currentLineItem.getUUID(), storeFront);
                }

            }

        }
        // store 'StorefrontCartLineItemsMap' in pipeline dictionary
        dict.put(DN_STOREFRONT_CART_LINE_ITEMS_MAP, storefrontCartLineItemsMap);
        
        return PIPELET_NEXT;
    }
}